package com.ly.sy.service.impl;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.sy.entity.Course;
import com.ly.sy.entity.Video;
import com.ly.sy.mapper.VideoMapper;
import com.ly.sy.service.VideoService;
import com.ly.sy.util.PutObjectProgressListener;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    //aliyun个人id和秘钥
    private static final String ACCESKEYID = "LTAI4GAKBF1CTWDEf6kqN7V7";
    private static final String ACCESSKEYSECRET = "NsPrj9vxTQO1LAhAo3IiaGSXiSXZdw";

    @Override
    public List<Video> BycourseId(Integer courseId) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<Video> CourseAndVideo = list(wrapper);
        return CourseAndVideo;
    }
    /**
     * @title:上传后的文件名称
     *
     * @fileName:上传文件的原始路径加名称
     *
     * @inputStream:上传文件的输入流
     *
     * **/
    @Override
    public String uploadvideotoaliy(MultipartFile file) {
        try{
            String fileName = file.getOriginalFilename();


            String title = fileName.substring(0,fileName.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            title = title+uuid;
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ACCESKEYID, ACCESSKEYSECRET, title, fileName, inputStream);

            System.out.println("==============================视频上传开始========================================");
            UploadVideoImpl uploader = new UploadVideoImpl();

            UploadStreamResponse response = uploader.uploadStream(request);

            System.out.println("RequesetId="+response.getRequestId());
            String videoId = "";
            if(response.isSuccess()){
                videoId = response.getVideoId();
                System.out.println("VideoId="+response.getVideoId());
            } else {
                videoId = response.getVideoId();
                System.out.println("VideoId="+response.getVideoId());
                System.out.println("ErrorCode="+response.getCode());
                System.out.println("ErrorMessage="+response.getMessage());
            }
            System.out.println("==============================视频上传结束========================================");
            return videoId;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<Video> videolikeLise(String searchstr, Integer courseId) {
        QueryWrapper<Video> queryWrapper=new QueryWrapper<>();

        queryWrapper.like("video_name", searchstr);
        queryWrapper.eq("course_id", courseId);
        List<Video> VideoFormSql= list(queryWrapper);
        return VideoFormSql;
    }
}
